package com.xmu.makerspace.service;

import com.xmu.makerspace.dao.FormalMemberRepository;
import com.xmu.makerspace.dao.FormalTeamRepository;
import com.xmu.makerspace.dao.SeatRepository;
import com.xmu.makerspace.domain.AuditStep;
import com.xmu.makerspace.domain.FormalMember;
import com.xmu.makerspace.domain.FormalTeam;
import com.xmu.makerspace.domain.Seat;
import com.xmu.makerspace.model.seat.SeatVO;
import com.xmu.makerspace.model.seat.SimpleMemberOfTeam;
import com.xmu.makerspace.model.seat.TeamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by status200 on 2017/9/8.
 */
@Service
public class SelectSeatService {

    private final SeatRepository seatRepository;

    private final FormalTeamRepository formalTeamRepository;

    private final FormalMemberRepository formalMemberRepository;

    private final AuditStepCheckService auditStepCheckService;

    @Autowired
    public SelectSeatService(SeatRepository seatRepository, FormalTeamRepository formalTeamRepository,
                             FormalMemberRepository formalMemberRepository, AuditStepCheckService auditStepCheckService) {
        this.seatRepository = seatRepository;
        this.formalTeamRepository = formalTeamRepository;
        this.formalMemberRepository = formalMemberRepository;
        this.auditStepCheckService = auditStepCheckService;
    }

    /**
     * 获取一个房间内所有座位的信息
     *
     * @param roomNo 房间号
     * @return
     */
    @Transactional(readOnly = true)
    public List<SeatVO> getAllSeatsInfo(int roomNo) {
        List<Seat> seats = seatRepository.findByRoomNo(roomNo);

        List<SeatVO> vos = new ArrayList<>();

        seats.forEach(e->{
            SeatVO vo = new SeatVO();
            vo.setSeatNo(e.getSeatNo());
            vo.setRoomNo(e.getRoomNo());
            vo.setMember(seatRepository.findSimpleMemberOfSeatByStudentId(e.getStudentId()));
            vos.add(vo);
        });

        return vos;
    }


    /**
     * 获取所有未选座的团队的信息
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<TeamVO> getAllTeamWithoutSeat() {
        List<FormalTeam> formalTeams = formalTeamRepository.findByAuditStep(AuditStep.NOT_SELECT_SEATS);

        List<TeamVO> vos = new ArrayList<>();

        formalTeams.forEach(e->{
            TeamVO vo = new TeamVO();
            vo.setTeamId(e.getTeamId());
            vo.setTeamName(e.getTeamName());

            List<FormalMember> members = formalMemberRepository.findByTeamId(e.getTeamId());
            List<SimpleMemberOfTeam> simpleMembers = new ArrayList<>();

            // 创建团队内成员的简单信息
            members.forEach(e1->{
                SimpleMemberOfTeam simpleMemberOfTeam = new SimpleMemberOfTeam();
                simpleMemberOfTeam.setStudentId(e1.getStudentId());
                simpleMemberOfTeam.setStudentName(e1.getStudentName());

                // 设置简单的座位信息
                simpleMemberOfTeam.setSeat(seatRepository.findSimpleSeatInfoByStudentId(e1.getStudentId()));

                // 设置该成员属于几个团队
                simpleMemberOfTeam.setBelongTeamNumber(formalMemberRepository.countByStudentId(e1.getStudentId()));

                simpleMembers.add(simpleMemberOfTeam);
            });
            vo.setMembers(simpleMembers);

            vos.add(vo);
        });

        return vos;
    }


    /**
     * 提交选座结果。
     * 由于一个学生可能在多个团队，因此一次选座可能会影响到其他团队的选座情况。
     * 所以提交结果后同时会检查所有团队的评审状态。
     *
     * @param seats
     */
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
    public void submit(List<Seat> seats) {
        seatRepository.save(seats);

        // 检查所有团队的座位评审状态
        auditStepCheckService.checkSeatState();

        // 检查所有团队的考勤号评审状态
        auditStepCheckService.checkAttendanceNumberState();
    }



}
